#include "server.h"
#include "ui_server.h"

#include <QTime>
#include <QDesktopWidget>
#include <QDebug>
#include <QFileInfo>

server::server(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::server)
{
    ui->setupUi(this);
    setWindowTitle("发送端");

    ui->progressBar->setValue(0);

    m_udpSocket =new QUdpSocket(this);
    m_udpSocket->setSocketOption(QAbstractSocket::SendBufferSizeSocketOption,8*1024*1024);
    m_udpSocket->setSocketOption(QAbstractSocket::ReceiveBufferSizeSocketOption,8*1024*1024);



}

server::~server()
{
    delete ui;
}

void server::on_pushButton_clicked()
{

    ui->progressBar->setValue(0);

    size=0;
    sendsize=0;

    qint64 mul=1024 * 62;//调试增加报文数据量，需要加上28个报文头，所以和初始化数据分开写
    //UDP最大支持的包大小为64KB，这里因为设置为63*1024+1024会有丢包出现，所以少传输一个KB字节

    qint64 c_init=1024 + mul;//数组初始化，注意996+28=1024，所以要保留1024

    QFile file("C:\\接受文件.pdf");//图片绝对路径，可修改
    if(!file.open(QIODevice::ReadOnly)) return ;

    char *m_sendBuf = new char[c_init];
    size=file.size();
    qDebug()<<"文件大小"<<size;


    int filetrans_size = 996 + mul ;

    int num = 0;
    int count = 0;
    int endSize = size %filetrans_size;


    if(endSize == 0){
        num = size/filetrans_size;
    }else{
        num = size/filetrans_size+1;
    }
    qint64 len = 0;

    while (count < num) {
        memset(m_sendBuf,0,c_init);
        ImageFrameHead mes;
        mes.funCode=24;
        mes.uTransFrameHdrSize = sizeof(ImageFrameHead);
        if((count+1) != num){
            mes.uTransFrameSize = filetrans_size;
        }else{
            mes.uTransFrameSize = endSize;
        }
        mes.uDataFrameSize = size;
        mes.uDataFrameTotal = num;
        mes.uDataFrameCurr = count+1;

        mes.uDataInFrameOffset = count*(c_init - sizeof(ImageFrameHead));
        qDebug()<<"uDataInFrameOffset"<<mes.uDataInFrameOffset;

        len = file.read(m_sendBuf+sizeof(ImageFrameHead),c_init-sizeof(ImageFrameHead));
        sendsize +=len;
        qDebug()<<"已发送文件大小"<<sendsize;

        memcpy(m_sendBuf , (char *) &mes,sizeof ( ImageFrameHead ) );

        m_udpSocket->writeDatagram(m_sendBuf,mes.uTransFrameSize + mes.uTransFrameHdrSize , QHostAddress("192.168.1.66"),65522);
        //接受文件的主机地址和端口，可修改


        QTime dieTime = QTime::currentTime().addMSecs(1);
        while( QTime::currentTime() < dieTime )
            QCoreApplication::processEvents(QEventLoop::AllEvents , 100 );
        count++;

        qint64 loadingSize = size - sendsize;

        float secend = (loadingSize/filetrans_size)/1000.0;
        //进度条
        ui->label->setText(
                    QString::asprintf("已发送%lldKB,发送速度为64KB/s,"
                                      "总共%lldKB,剩余%lldKB,剩余时间%.1fs",
                                      sendsize /1024 , size /1024 , loadingSize /1024 , secend )
                    );

        ui->progressBar->setMaximum(size);
        ui->progressBar->setValue(sendsize);

    }

    qDebug()<<"数据发送完成";
    file.close();
    QTime dieTime = QTime::currentTime().addMSecs(10);
    while( QTime::currentTime() < dieTime )
        QCoreApplication::processEvents(QEventLoop::AllEvents , 100 );



}















